package cn.itqiqi.student_system_java.service.impl;

import cn.itqiqi.student_system_java.mapper.CourseMapper;
import cn.itqiqi.student_system_java.pojo.Course;
import cn.itqiqi.student_system_java.pojo.CourseVo;
import cn.itqiqi.student_system_java.service.CourseService;
import cn.itqiqi.student_system_java.utils.ResultUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author kevin_琪琪
 * @version 1.0
 * @description
 * @date 2022-06-20 21:13
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ResultUtils resultUtils;

    @Override
    public String getCourseAll(Long pageNum, Long pageSize) {
        Page<Course> coursePage = new Page<>(pageNum, pageSize);
        List<Course> courseAll = courseMapper.getCourseAll(coursePage);
        CourseVo courseVo = new CourseVo(coursePage.getTotal(), courseAll);
        return resultUtils.success(courseVo);
    }

    @Override
    public String getCourseByName(Long pageNum, Long pageSize, String queryValue) {
        if (queryValue == null || queryValue.equals("")) {
            return resultUtils.fail(400, "参数不合法！");
        } else {
            Page<Course> coursePage = new Page<>(pageNum, pageSize);
            List<Course> courseAll = courseMapper.getCourseByName(coursePage, queryValue);
            CourseVo courseVo = new CourseVo(coursePage.getTotal(), courseAll);
            return resultUtils.success(courseVo);
        }

    }

    @Override
    public String addCourse(Course course) {
        if (course.getCname() == null || course.getCnum() == null) {
            return resultUtils.fail(400, "参数不合法!");
        } else {
            try {
                courseMapper.addCourse(course);
                return resultUtils.success("添加成功！");
            } catch (Exception e) {
                return resultUtils.fail(406, "添加失败");
            }
        }
    }

    @Override
    public String updateCourse(Course course) {
        if (course.getCid() == null) {
            return resultUtils.fail(400, "参数不合法!");
        } else {
            try {
                courseMapper.updateCourse(course);
                return resultUtils.success("修改成功！");
            } catch (Exception e) {
                return resultUtils.fail(406, "修改失败");
            }
        }
    }

    @Override
    public String deleteCourse(Course course) {
        if (course.getCid() == null) {
            return resultUtils.fail(400, "参数不合法！");
        } else {
            try {
                courseMapper.deleteCourse(Integer.parseInt(course.getCid().toString()));
                return resultUtils.success("删除成功！");
            } catch (Exception e) {
                return resultUtils.fail(406, "删除失败！");
            }
        }
    }
}
